package org.projsquirrel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.projsquirrel.R.drawable;
import org.w3c.dom.Text;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.text.Editable;
import android.util.Log;
import android.view.*;
import android.location.*;

@SuppressWarnings("unused")
public class SurveyActivity extends ListActivity
{
	private Context localInstance;
	//this section is the section for answers from the survey, makes sure we don't loose the address to the objects
	private HashMap<String,EditText> sqrAnswerArray;
	private HashMap<String,TextView> answerArray;
	private ArrayList<String> Questions;
	private ArrayList<RelativeLayout> SQRL;
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    private TextView Time, textTime, Date, textDate, Zip, textZip;
    //Your Opinion...
    private TextView email, answerEmail, answerDelta, deltaSquirrel, answerTSite, thisSite, answerOSite, 
    	otherSite, grayS, foxS, setting, answerSet, trees, answerTrees, food, answerFood,
    	DogCat, answerDC, Pred, answerPred, comments;
    private EditText answerGS, answerFS, answerComment;
    private Button submit;

    private RelativeLayout mainLayout;
    private RelativeLayout timeLayout, dateLayout, zipLayout, emailLayout, deltaLayout, tsiteLayout, ositeLayout,
    	Squirrels, Setting, Trees, Food, DC, Predators, Comments;
    private int hour, minute, Year, month, day;
    static final int MAP_ACTIVITY = 100;
    static final int TIME_DIALOG_ID = 0;
    static final int DATE_DIALOG_ID = 1;
    static final int EMAIL_DIALOG_ID = 2;
    static final int DELTA_DIALOG_ID = 3;
    static final int THISSITE_DIALOG_ID = 4;
    static final int OTHERSITE_DIALOG_ID = 5;
    static final int SITE_DIALOG_ID = 6;
    
    public LocationManager locationManager;
    private Canvas imagecanvas;
    private Button myButton;
    
    private ListView myListView;
    private ArrayList<View> views;
    public MyIndexerAdapter<View> adapter;
    
/////scroll////
    class MyIndexerAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
    	
        //views version
        ArrayList<View> myViews;
        HashMap<View, Integer> viewIndexer;
        View[] Views;
        int resource;
        
        public ArrayList getList()
        {
        	//return myElements;
        	return myViews;
        }
        
        public HashMap getElements()
        {
        	//return alphaIndexer;
        	return viewIndexer;
        }

        public View[] pickViews() //views version
        {
        	return Views;
        }
        
        @SuppressWarnings("unchecked")
		public MyIndexerAdapter(Context context, int textViewResourceId,
                        List<T> objects) {
                super(context, textViewResourceId, objects);
                //resource = textViewResourceId;
              
                //views version
                myViews = (ArrayList<View>) objects;
                viewIndexer = new HashMap<View, Integer>();
                int Size = views.size();
                for (int j = Size - 1; j >= 0; j--) {
                     View v = views.get(j);
                     viewIndexer.put(v, j); 
                } 
                Set<View> keys = viewIndexer.keySet(); // indexer

                Iterator<View> it = keys.iterator();
                ArrayList<View> keyList = new ArrayList<View>();
                while (it.hasNext()) {
                        View key = it.next();
                        keyList.add(key);
                }

                //Collections.sort(keyList);
                Views = new View[keyList.size()]; 
                keyList.toArray(Views);

        }


        //@Override
        public int getPositionForSection(int section) {
        		View v = Views[section];
        		return viewIndexer.get(v);

        }

        //@Override
        public int getSectionForPosition(int position) {
                Log.v("getSectionForPosition", "called");
                return 0;
        }

        //@Override
        public Object[] getSections() {
        		return Views;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        	RelativeLayout rl;
            if (convertView == null) {
                 LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 rl = (RelativeLayout) vi.inflate(R.layout.item, null, true);  	
                 if(position == 0)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(Time.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(textTime.getText().toString());
                 }
                 if(position == 1)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(Date.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(textDate.getText().toString());
                 }
                 if(position == 2)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(Zip.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(textZip.getText().toString());
                 }
                 if(position == 3)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(deltaSquirrel.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(answerDelta.getText().toString());
                 }
                 if(position == 4)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(thisSite.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(answerTSite.getText().toString());
                 }
                 if(position == 5)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(otherSite.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(answerOSite.getText().toString());
                 }
                 if(position == 6)
                 {
                	 TextView q = (TextView)rl.findViewById(R.id.question);
                	 q.setText(email.getText().toString());
                	 TextView a = (TextView)rl.findViewById(R.id.answer);
                	 a.setText(answerEmail.getText().toString());
                 }
                /*
            	if(position == 7)
                 	 rl = (RelativeLayout)myViews.get(7);
                if(position == 8)
                 	 rl = (RelativeLayout)myViews.get(8);
                if(position == 9)
                 	 rl = (RelativeLayout)myViews.get(9);
                if(position == 10)
                 	 rl = (RelativeLayout)myViews.get(10);
                if(position == 11)
                 	 rl = (RelativeLayout)myViews.get(11);
                if(position == 12)
                 	 rl = (RelativeLayout)myViews.get(12);
                if(position == 13)
                 	 rl = (RelativeLayout)myViews.get(13);
                if(position == 14)
                 	 rl = (RelativeLayout)myViews.get(14);
                if(position == 15)
                 	 rl = (RelativeLayout)myViews.get(15);
                 	 */
            } 
            else
            	rl = (RelativeLayout)convertView;
            
            
            return rl;
       }

} //index adapter class
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.survey);

        //LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//View myView = linflater.inflate(R.layout.content, null);
		
        this.sqrAnswerArray = new HashMap<String, EditText>();
        this.answerArray = new HashMap<String, TextView>();
        this.Questions = new ArrayList<String>();
        localInstance = this;
        
        //location manager initialization
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
		//relative layouts for each survey section
		timeLayout = (RelativeLayout)findViewById(R.id.relativelayout_time);
		dateLayout = (RelativeLayout)findViewById(R.id.relativelayout_date);
		zipLayout = (RelativeLayout)findViewById(R.id.relativelayout_zip);
		deltaLayout = (RelativeLayout)findViewById(R.id.relativelayout_delta);
		tsiteLayout = (RelativeLayout)findViewById(R.id.relativelayout_thisSite);
		ositeLayout = (RelativeLayout)findViewById(R.id.relativelayout_otherSite);
		emailLayout = (RelativeLayout)findViewById(R.id.relativelayout_email);
		Squirrels = (RelativeLayout)findViewById(R.id.relativelayout_number);
		Setting = (RelativeLayout)findViewById(R.id.relativelayout_setting);
		Trees = (RelativeLayout)findViewById(R.id.relativelayout_trees);
		Food = (RelativeLayout)findViewById(R.id.relativelayout_food);
		DC = (RelativeLayout)findViewById(R.id.relativelayout_dc);
		Predators = (RelativeLayout)findViewById(R.id.relativelayout_pred);
		Comments = (RelativeLayout)findViewById(R.id.relativelayout_comments);
		
		//survey sections
		Time = (TextView)findViewById(R.id.table_time);
        textTime = (TextView)findViewById(R.id.text_time);
        
        Date = (TextView)findViewById(R.id.table_date);
        textDate = (TextView)findViewById(R.id.text_date);
        
        Zip = (TextView)findViewById(R.id.table_zip);
        textZip = (TextView)findViewById(R.id.text_zip);
 
        grayS = (TextView)findViewById(R.id.gray);
        answerGS = (EditText)findViewById(R.id.answer_gray);
        
        foxS = (TextView)findViewById(R.id.fox);
        answerGS = (EditText)findViewById(R.id.answer_fox);
        
        setting = (TextView)findViewById(R.id.setting);
        answerSet = (TextView)findViewById(R.id.answer_setting);
        
        trees = (TextView)findViewById(R.id.trees);
        answerTrees = (TextView)findViewById(R.id.answer_trees);
        
        food = (TextView)findViewById(R.id.food);
        answerFood = (TextView)findViewById(R.id.answer_food);
        
        DogCat = (TextView)findViewById(R.id.dogcat);
        answerDC = (TextView)findViewById(R.id.answer_dogcat);
        
        Pred = (TextView)findViewById(R.id.pred);
        answerPred = (TextView)findViewById(R.id.answer_pred);
        
        comments = (TextView)findViewById(R.id.comments);
        answerComment = (EditText)findViewById(R.id.answer_comment);
        
        deltaSquirrel = (TextView)findViewById(R.id.text_deltaSquirrel);
        answerDelta = (TextView)findViewById(R.id.answer_deltaSquirrel);
        
        thisSite = (TextView)findViewById(R.id.text_thisSite);
        answerTSite = (TextView)findViewById(R.id.answer_thisSite);
        
        otherSite = (TextView)findViewById(R.id.text_otherSite);
        answerOSite = (TextView)findViewById(R.id.answer_otherSite);
        
        email = (TextView)findViewById(R.id.text_email);
        //answerEmail = (TextView)findViewById(R.id.answer_email);
        
        submit = (Button)findViewById(R.id.button_submit); //submit button
       
	    //populate views array
        views = new ArrayList<View>(); 
	    views.add(timeLayout);
	    views.add(dateLayout);
	    views.add(zipLayout);
	    views.add(deltaLayout);
	    views.add(tsiteLayout);
	    views.add(ositeLayout);
	    views.add(emailLayout);
	    views.add(Squirrels);
		views.add(Setting);
		views.add(Trees);
		views.add(Food);
		views.add(DC);
		views.add(Predators);
		views.add(Comments);
	    /*
	    //load questions here
	    this.Questions.add("Please list the number of individual Grey squirrels observed at the observation site on the observation date.");
	    this.Questions.add("Please list the number of individual Fox squirrels observed at the observation site on the observation date.");
	    
	    this.Questions.add("Are there any Nut-bearing trees at the observation site?");
	    this.Questions.add("Are there any Seed-bearing trees at the observation site?");
	    this.Questions.add("Are there any Fruit-bearing trees at the observation site?");
	    this.Questions.add("Are there any Tiny-seeded trees at the observation site?");
	    this.Questions.add("Are there any Coniferous trees at the observation site?");
	    
	    this.Questions.add("Do you observe squirrels regularly obtaining food from Bird Feeders?");
	    this.Questions.add("Do you observe squirrels regularly obtaining food from Human Handouts?");
	    this.Questions.add("Do you observe squirrels regularly obtaining food from Garbage cans?");
	    this.Questions.add("Do you observe squirrels regularly obtaining food from Trees and other plants?");
	    
	    this.Questions.add("How often are cats near the observation site? (This includes cats that live at the site, strays, animals that visit the site on a walk, and others.)");
	    this.Questions.add("How often are dogs near the observation site? (This includes dogs that live at the site, strays, animals that visit the site on a walk, and others.)");
	    
	    this.Questions.add("How often are Coyotes seen near the observation site?");
	    this.Questions.add("How often are Hawks seen near the observation site?");
	    this.Questions.add("How often are Poisoned Grain seen near the observation site?");
	    
	    //TODO BRY
	    //bry load your hook code here
	    //TODO BRY
	    
	    //run isaw here
	    this.iSawFactorysqr(7, 7, this.Questions.get(1), R.drawable.fox, "NO INFO", views);
	    this.iSawFactorysqr(8, 8, this.Questions.get(0), R.drawable.grey, "NO INFO", views);
	    int file = 0;
	    int type = 0;
	    
	    for(int i = 9; i<this.Questions.size()+9-2; i++)
	    {	
	    	switch(i-9+2)
	    	{
	    	case 2:
	    		//Nut-bearing
	    		type = 3;
	    		file = R.drawable.walnut;
	    		break;
	    	case 3:
	    		//Seed-bearing
	    		type = 3;
	    		file = R.drawable.maple;
	    		break;
	    	case 4:
	    		//Fruit-bearing
	    		type = 3;
	    		file = R.drawable.gingko_biloba_tree;
	    		break;
	    	case 5:
	    		//Tiny-seeded
	    		type = 3;
	    		file = R.drawable.willow_tree;
	    		break;
	    	case 6:
	    		//Coniferous
	    		type = 3;
	    		file = R.drawable.spruce;
	    		break;
	    		
	    	case 7:
	    		//Bird Feeders
	    		type = 2;
	    		file = R.drawable.birdfeeder;
	    		break;
	    	case 8:
	    		//Human Handouts
	    		type = 2;
	    		file = R.drawable.human;
	    		break;
	    	case 9:
	    		//Garbage cans
	    		type = 2;
	    		file = R.drawable.trash;
	    		break;
	    		
	    	case 10:
	    		//Trees and other plants
	    		type = 2;
	    		file = R.drawable.pine;
	    		break;
	    	
	    	case 11:
	    		//cats
	    		type = 1;
	    		file = R.drawable.cat;
	    		break;
	    	case 12:
	    		//dogs
	    		type = 1;
	    		file = R.drawable.dog;
	    		break;
	    	case 13:
	    		//Coyotes
	    		type = 1;
	    		file = R.drawable.coyote;
	    		break;
	    	case 14:
	    		//Hawks
	    		type = 1;
	    		file = R.drawable.hawk;
	    		break;
	    	case 15:
	    		//Poisoned
	    		type = 1;
	    		file = R.drawable.grain;
	    		break;
	    		
	    		
	    	}
	    	this.iSawFactory(type, i, i, this.Questions.get(i-9+2), file, "NO INFO");
	    }
	    */
        myListView = (ListView) findViewById(android.R.id.list);
        myListView.setFastScrollEnabled(true);
        myListView.setCacheColorHint(0);
        adapter = new MyIndexerAdapter<View>(
                        getApplicationContext(), android.R.layout.simple_list_item_2,
                        views);
        myListView.setAdapter(adapter);
        
        final Calendar c = Calendar.getInstance();
        
        //set the defaults for the time
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        
        //set if its AM or PM
        String amPm;
        if(hour > 11)
        	amPm = "PM";
        else
        	amPm = "AM";
        
        //format the hours
        if(hour > 12)
        	hour -= 12;
        if(hour == 0)
        	hour = 12;
        
        //format the minutes
        String zero;
        if(minute < 10)
        	zero = "0";
        else
        	zero = "";
        
        //set the textTime            
        textTime.setText(hour + ":" + zero + minute + " " + amPm);
        String time = textTime.toString();
        
        //set the defaults for the date
        Year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        textDate.setText(month + 1 + "/" + day + "/" + Year);
        String date = textDate.toString();
        
        /* SECTION CAUSES A CRASH
		//set default zip code
		Location current = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		Geocoder geo = new Geocoder(this,Locale.getDefault());
		List<Address> addresses;
		try {
			addresses = geo.getFromLocation(current.getLatitude(), current.getLongitude(), 1);
			if(addresses != null && addresses.get(0).getPostalCode() != null)
				textZip.setText(addresses.get(0).getPostalCode());
			else
				textZip.setText("Select location from map");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	*/
    
        submit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Toast.makeText(getApplicationContext(), "Submitted", Toast.LENGTH_SHORT).show();
        		createReport();
        	}
        });
   	}
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if(position == 0)
			showDialog(TIME_DIALOG_ID);
		if(position == 1)
			showDialog(DATE_DIALOG_ID);
		if(position == 2)
		{
			Intent intent = new Intent(localInstance, Map.class);
			startActivityForResult(intent, MAP_ACTIVITY);
		}
		if(position == 3)
		{
			showDialog(DELTA_DIALOG_ID);
		}
		if(position == 4)
			showDialog(THISSITE_DIALOG_ID);
		if(position == 5)
			showDialog(OTHERSITE_DIALOG_ID);
		if(position == 6)
			showDialog(EMAIL_DIALOG_ID);
	}
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if(requestCode == MAP_ACTIVITY && resultCode == RESULT_OK) //the map activity has returned and it supplied a new location
    	{
    		textZip.setText(data.getCharSequenceExtra("zip"));    		
    	}
    }    
    
	protected void createReport() 
	{
	
	}	

    protected Dialog onCreateDialog(int id)
    {
    	switch(id)
    	{
    	case TIME_DIALOG_ID:
    		return new TimePickerDialog(this,mTimeSetListener, hour, minute, false);
    	case DATE_DIALOG_ID:
    		return new DatePickerDialog(this,mDateSetListener, Year, month, day);
    	case EMAIL_DIALOG_ID:
    		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            final EditText input = new EditText(this);
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	String value = input.getText().toString().trim();
                    Toast.makeText(getApplicationContext(), value,
                    Toast.LENGTH_SHORT).show();
                }
            });

            alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
            });
            return alert.show(); 
	    case DELTA_DIALOG_ID:
	    	final String[] delta_items = {"Increased", "Decreased", "Stayed the Same"};
	    	AlertDialog.Builder delta_builder = new AlertDialog.Builder(this);
	    	delta_builder.setTitle("Number has");
	    	delta_builder.setItems(delta_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	answerDelta.setText(delta_items[item]);
	    	    	myListView.setAdapter(adapter);
	    	    	adapter.setNotifyOnChange(true);
	    	    }
	    	});
	    	AlertDialog delta_alert = delta_builder.create();
	    	return delta_alert;
	    //Alternate Multiple Choice
	    case SITE_DIALOG_ID:
	    	final CharSequence[] site_items = {"This Location", "Other Locations"};
	    	AlertDialog.Builder site_builder = new AlertDialog.Builder(this);
	    	site_builder.setTitle("Have logged for");
	    	site_builder.setMultiChoiceItems(site_items, null, new DialogInterface.OnMultiChoiceClickListener() {
	    	    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
	    	    	//answerTSite.setText(site_items[which]);
	    	    }
	    	});
	    	AlertDialog site_alert = site_builder.create();
	    	return site_alert;
	    	
	    case OTHERSITE_DIALOG_ID:
	    	final String[] otherSite_items = {"Yes", "No"};
	    	AlertDialog.Builder otherSite_builder = new AlertDialog.Builder(this);
	    	otherSite_builder.setTitle("Logged other locations");
	    	otherSite_builder.setItems(otherSite_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	answerOSite.setText(otherSite_items[item]);
	    	    	myListView.setAdapter(adapter);
	    	    	adapter.setNotifyOnChange(true);
	    	    }
	    	});
	    	AlertDialog otherSite_alert = otherSite_builder.create();
	    	return otherSite_alert;
	    case THISSITE_DIALOG_ID:
	    	final String[] thisSite_items = {"Yes", "No"};
	    	AlertDialog.Builder thisSite_builder = new AlertDialog.Builder(this);
	    	thisSite_builder.setTitle("Logged this location");
	    	thisSite_builder.setItems(thisSite_items, new DialogInterface.OnClickListener() {
	    	    public void onClick(DialogInterface dialog, int item) {
	    	    	answerTSite.setText(thisSite_items[item]);
	    	    	myListView.setAdapter(adapter);
	    	    	adapter.setNotifyOnChange(true);
	    	    }
	    	});
	    	AlertDialog thisSite_alert = thisSite_builder.create();
	    	return thisSite_alert;
    	}
    	return null;
    }
    
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
    {		
		public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
		{
            hour = hourOfDay;
            minute = minuteOfHour;  
            
            //set if its AM or PM
            String amPm;
            if(hour > 11)
            	amPm = "PM";
            else
            	amPm = "AM";
            
            //format the hours
            if(hour > 12)
            	hour -= 12;
            if(hour == 0)
            	hour = 12;
            
            //format the minutes
            String zero;
            if(minute < 10)
            	zero = "0";
            else
            	zero = "";
            
            //set the textTime            
            textTime.setText(hour + ":" + zero + minute + " " + amPm);
            myListView.setAdapter(adapter);
	    	adapter.setNotifyOnChange(true);
		}
	};
	
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
    {
         public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
         {
        	 Year = year;
             month = monthOfYear;
             day = dayOfMonth;
             textDate.setText(month + 1 + "/" + day + "/" + Year);
             myListView.setAdapter(adapter);
 	    	 adapter.setNotifyOnChange(true);
          }
     };
    
	 	private void iSawFactory(final int type, int id, int position, String question, int drawable, String expandedInfo)
	{
//		System.out.println(this.adapter.alphaIndexer.size());
//		Key[] array = null;
//		this.adapter.alphaIndexer.keySet().toArray(array);
//		HashMap<String, Integer> mymap = this.adapter.alphaIndexer;
//		for(int v = 0; v<this.adapter.alphaIndexer.size(); v++)
//		{
//			System.out.println(mymap.get(array[v]));
//		}
		//position should be greater than 6.
		//this section still needs testing.
//	    LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid); //this is the base layout ie survey
//	    View rowView = getLayoutInflater().inflate(R.layout.row, null);
//	    LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
 		char[] defaultValue = {'0'};
 		switch(type)
		{
		case 1:
			defaultValue = new char[] {'N','O','N','E'};
			break;
		case 2:
			defaultValue = new char[] {'N','E','V','E','R'};
			break;
		case 3:
			defaultValue = new char[] {'Y','E','S'};
			break;
		}
		char[] questionText = question.toCharArray();
		// TODO Auto-generated method stub
		//RelativeLayout survey = (RelativeLayout) findViewById(R.id.widget211);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //dont thing i need this
		View myView = linflater.inflate(R.layout.survey_element, null);
		
        final TextView t = (TextView) myView.findViewById(R.id.Text);
        TextView q = (TextView) myView.findViewById(R.id.question);
        ImageView i = (ImageView) myView.findViewById(R.id.image);
        Button btnExpand = (Button) myView.findViewById(R.id.btnExpand);
        ImageButton btnDwn = (ImageButton) myView.findViewById(R.id.btnDwn);
        ImageButton btnUp = (ImageButton) myView.findViewById(R.id.btnUp);
        
        q.setText(questionText, 0, questionText.length);
        t.setText(defaultValue, 0, defaultValue.length);
        i.setImageResource(drawable);
        
        //there might be an issue with the ID's here during runtime need to look into a better way of doing this.
        t.setId(id*2+id);
        this.answerArray.put(question, t);
        q.setId(id*2*id*2*id*2*id*2 + id);
        i.setId(id*2*id*2 + id);
        btnExpand.setId(id*2*id*2*id*2 + id);
        btnDwn.setId(id*2*id*2*id*2*id*2*id*2 + id);
        btnUp.setId(id*2*id*2*id*2*id*2*id*2*id*2 + id);
        
        btnExpand.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO get new intent and display more info about image

			}
		});
        btnDwn.setImageResource(R.drawable.down_arrow_48);
		btnDwn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				String temp = t.getText().toString();
				switch (type)
				{
				case 1:
					if(temp.equalsIgnoreCase("NONE"))
					{
						//temp = "LOW";
							//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("LOW"))
					{
						temp = "NONE";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("MEDIUM"))
					{
						temp = "LOW";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("HIGH"))
					{
						temp = "MEDIUM";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				case 2:
					if(temp.equalsIgnoreCase("NEVER"))
					{
						//temp = "SELDOM";
							//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("SELDOM"))
					{
						temp = "NEVER";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("OFTEN"))
					{
						temp = "SELDOM";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("REGULARLY"))
					{
						temp = "OFTEN";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				case 3:
					if(temp.equalsIgnoreCase("YES"))
					{
						temp = "NO";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("NO"))
					{
						temp = "UNSURE";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("UNSURE"))
					{
						//temp = "";
						//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				}
				
			}
		});
		btnUp.setImageResource(R.drawable.up_arrow_48);
		btnUp.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				String temp = t.getText().toString();
				switch (type)
				{
				case 1:
					if(temp.equalsIgnoreCase("NONE"))
					{
						temp = "LOW";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("LOW"))
					{
						temp = "MEDIUM";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("MEDIUM"))
					{
						temp = "HIGH";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("HIGH"))
					{
						//temp = "MEDIUM";
						//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				case 2:
					if(temp.equalsIgnoreCase("NEVER"))
					{
						temp = "SELDOM";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("SELDOM"))
					{
						temp = "OFTEN";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("OFTEN"))
					{
						temp = "REGULARLY";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("REGULARLY"))
					{
						//temp = "OFTEN";
						//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				case 3:
					if(temp.equalsIgnoreCase("YES"))
					{
						//temp = "NO";
						//t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("NO"))
					{
						temp = "YES";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					if(temp.equalsIgnoreCase("UNSURE"))
					{
						temp = "NO";
						t.setText(temp.toCharArray(),0,temp.toCharArray().length);
					}
					break;
				}
			}
		});
		//TODO this line needs to be commented out later to make IsawFactory work
      //this.adapter.alphaIndexer.put( myView, position);
		//SQRL.add((RelativeLayout) myView);
		views.add(myView);
		
	}
     
	private void iSawFactorysqr(int id, int position, String question, int drawable, String expandedInfo, ArrayList<View> views)
	{
//		System.out.println(this.adapter.alphaIndexer.size());
//		Key[] array = null;
//		this.adapter.alphaIndexer.keySet().toArray(array);
//		HashMap<String, Integer> mymap = this.adapter.alphaIndexer;
//		for(int v = 0; v<this.adapter.alphaIndexer.size(); v++)
//		{
//			System.out.println(mymap.get(array[v]));
//		}
		//position should be greater than 6.
		//this section still needs testing.
//	    LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid); //this is the base layout ie survey
//	    View rowView = getLayoutInflater().inflate(R.layout.row, null);
//	    LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
		char[] defaultValue = {'0'};
		char[] questionText = question.toCharArray();
		// TODO Auto-generated method stub
		//RelativeLayout survey = (RelativeLayout) findViewById(R.id.widget211);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //dont thing i need this
		View myView = new View(localInstance); 
		myView = linflater.inflate(R.layout.survey_squirrel, null);
		
        final EditText t = (EditText) myView.findViewById(R.id.sqr_Text);
        TextView q = (TextView) myView.findViewById(R.id.sqr_question);
        ImageView i = (ImageView) myView.findViewById(R.id.sqr_image);
        Button btnExpand = (Button) myView.findViewById(R.id.sqr_btnExpand);
        ImageButton btnDwn = (ImageButton) myView.findViewById(R.id.sqr_btnDwn);
        ImageButton btnUp = (ImageButton) myView.findViewById(R.id.sqr_btnUp);
        
        q.setText(questionText, 0, questionText.length);
        t.setText(defaultValue, 0, defaultValue.length);
        i.setImageResource(drawable);
        
        //there might be an issue with the ID's here during runtime need to look into a better way of doing this.
        t.setId(id);
        this.sqrAnswerArray.put(question,t);
        q.setId(id);
        i.setId(id);
        btnExpand.setId(id);
        btnDwn.setId(id);
        btnUp.setId(id);
        
        btnExpand.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO get new intent and display more info about image

			}
		});
        btnDwn.setImageResource(R.drawable.down_arrow_48);
		btnDwn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				String temp = t.getText().toString();
				int i = Integer.getInteger(temp);
				if(i>0)
				{
				i--;
				temp = ""+i;
				t.setText(temp.toCharArray(),0,temp.toCharArray().length);
				}
			}
		});
		btnUp.setImageResource(R.drawable.up_arrow_48);
		btnUp.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				String temp = t.getText().toString();
				int i = Integer.getInteger(temp);
				i++;
				temp = ""+i;
				t.setText(temp.toCharArray(),0,temp.toCharArray().length);
			}
		});
		//TODO this line needs to be commented out to make IsawFactorysqr work.
      //this.adapter.alphaIndexer.put( myView, position);
		views.add(myView);
		//SQRL.add((RelativeLayout) myView);
		
	}
}